using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using AutoGear.BLL;
using ECommerce.Library;
using Entity;
using ECommerce.DAL;

public partial class invoice_print_page : SitePublicBasePage
{
    private ProductPurchaseOrder _order;
    public ProductPurchaseOrder Order
    {
        get
        {
            if (_order == null)
            {
                string orderNumber = string.Empty;
                if(AutoGearSessionManager.OrderNumber != null)
                    orderNumber = AutoGearSessionManager.OrderNumber;
                else if (Request["oid"] != null && !String.IsNullOrEmpty(Request["oid"]))
                    orderNumber = Request["oid"];
                _order = new ProductPurchaseOrder(orderNumber);
                AutoGearSessionManager.OrderNumber = null;
            }
            return _order;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            if (Order == null)
            {
                Response.Redirect(AutoGear.BLL.CustomSiteMap.OrderSummary);
            }
            CustomCollection<TblGiftCertificateOrderItems> Giftitems = TblGiftCertificateOrderItems.GetTblGiftCertificateOrderItemsByOrderId(_order.OrderId);

            if (Order.ShoppingCart == null || Order.ShoppingCart.CartItems.Count <= 0)
            {
                if (Giftitems == null)
                {
                    Response.Redirect(AutoGear.BLL.CustomSiteMap.ShoppingCart);
                }
            }
            BindData();
        }
    }

    private void BindData()
    {
        BindProductList();

        CreditCard card = (CreditCard)Order.CreditCard;
        ShoppingCart cart = Order.ShoppingCart;
        AutoGear.BLL.Address billingAddress = (AutoGear.BLL.Address)Order.BillingAddress;
        AutoGear.BLL.Address shippingAddress = (AutoGear.BLL.Address)Order.ShippingAddress;
        lblRequestDeliveryDate.Text = Order.RequestedDeliveryDate.ToShortDateString();
        string Shippingmethod = string.Empty;
        if (Order.ProductShipping.ProductShippingMethod == "1")
        {
            Shippingmethod = "Ground";
        }
        else if (Order.ProductShipping.ProductShippingMethod == "2")
        {
            Shippingmethod = "3 Day Select";
        }
        else if (Order.ProductShipping.ProductShippingMethod == "3")
        {
            Shippingmethod = "2nd Day Air";
        }
        else if (Order.ProductShipping.ProductShippingMethod == "4")
        {
            Shippingmethod = "Next Day Air";
        }
        lblShipmentStatus.Text = Shippingmethod;
        lblDate.Text = Order.OrderDate.ToShortDateString();
        lblOrderNo.Text = Order.OrderNumber;
        lblSubTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(cart.SubTotal + cart.GiftSubTotal);
        lblCouponDiscount.Text = "-" + ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.CouponDiscount);
        lblShippingHandling.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.ProductShipping.ProductShippingCost);
        lblTax.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.TaxAmount);
        lblTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(Order.TotalAmount);

        lblBillingFirstName.Text = billingAddress.FirstName;
        lblBillingLastName.Text = billingAddress.LastName;
        lblBillingEmailAddress.Text = billingAddress.EmailAddress;
        lblBillingAddress.Text = billingAddress.AddressLine1 + "\n" + billingAddress.AddressLine2;
        if (billingAddress.TelephoneNumber.Length > 3)
            lblBillingPhone.Text = "Primary Phone:" + billingAddress.TelephoneNumber;
        if (billingAddress.TelephoneNumber2.Length > 3)
            lblBillingPhone2.Text = "Secondary Phone: " + billingAddress.TelephoneNumber2;
        lblBillingState.Text = billingAddress.AddressState;
        lblBillingCity.Text = billingAddress.City;
        lblBillingZipCode.Text = billingAddress.ZipCode;
        
        lblShippingFirstName.Text = shippingAddress.FirstName;
        lblShippingLastName.Text = shippingAddress.LastName;
        lblShippingAddress.Text = shippingAddress.AddressLine1 + "\n" + shippingAddress.AddressLine2;
        lblShippingEmailAddress.Text = shippingAddress.EmailAddress;
        lblShippingPhone.Text = shippingAddress.TelephoneNumber;
        lblShippingCity.Text = shippingAddress.City;
        lblShippingState.Text = shippingAddress.AddressState;
        lblShippingZipCode.Text = shippingAddress.ZipCode;
        lblShippingState.Text = shippingAddress.AddressState;
        //if (card != null)
        //{
        //    if (Order.CreditCardAmount > 0)
        //        lblCreditCard.Text = String.Format("{0:C}", Order.CreditCardAmount) + "has been deducted from " + card.CardType + " ending in " + SiteUtility.HideCharacter(card.CardNumber, '*', card.CardNumber.Length - 4, true);
        //}
    }

    private void BindProductList()
    {
        
        ShoppingCart cart = Order.ShoppingCart;
        rptProduct.DataSource = cart.CartItems;
        rptProduct.DataBind();

        CustomCollection<TblGiftCertificateOrderItems> gftItems = TblGiftCertificateOrderItems.GetTblGiftCertificateOrderItemsByOrderId(_order.OrderId);
        if (gftItems != null)
        {
            int temp_code = gftItems[0].GiftCertificateId;
            TblGiftCertificate CurrentGiftCertificate = TblGiftCertificate.GetTblGiftCertificateByGiftCertificateId(temp_code);

            GiftItem giftItem = new GiftItem(CurrentGiftCertificate.GiftCertificateId.ToString(), CurrentGiftCertificate.Code, CurrentGiftCertificate.FromEmailAddress, CurrentGiftCertificate.FromName,
                CurrentGiftCertificate.ToEmailAddress, CurrentGiftCertificate.ToName, CurrentGiftCertificate.Message, CurrentGiftCertificate.Quantity, CurrentGiftCertificate.PriceAmount, CurrentGiftCertificate.DateCreated, CurrentGiftCertificate.DateExpired);

            cart.AddGiftItem(giftItem);

            rptGiftCertificate.DataSource = cart.GiftItems;
            rptGiftCertificate.DataBind();
        }

        if (rptProduct.Items.Count == 0) 
        {
            rptProduct.Visible = false;
        }
        if (rptGiftCertificate.Items.Count == 0) 
        {
            rptGiftCertificate.Visible = false;
        }
        if (Order.Substitute.Length > 5) 
        {
            //lblSubstitute.Text = Order.Substitute;
            //lblSubstitute.Visible = false;
        }

        //imgGIftCard.ImageUrl = Order.GiftCardImageUrl;
        if (!String.IsNullOrEmpty(Order.SpecialInstruction))
        {
            lblComments.Visible = true;
            lblComments.Text = "<b>Comments</b> :  " + Order.SpecialInstruction;
            lblComments.Style.Add("height", "250px");
        }
        if (!String.IsNullOrEmpty(Order.GIftCardMessage))
        {
            lblGiftCardMsg.Visible = true;
            lblGiftCardMsg.Text = "<b>Gift Message</b> :  " + Order.GIftCardMessage;
            lblGiftCardMsg.Style.Add("height", "250px");
        }
        if (!String.IsNullOrEmpty(Order.OfficeUse))
        {
            lblOfficeUse.Visible = true;
            lblOfficeUse.Text = "<b>Office Use</b> :  " + Order.OfficeUse;
            lblOfficeUse.Style.Add("height", "250px");
        }        
        
    }

    protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            CartItem cartItem = (CartItem)e.Item.DataItem;
            if (cartItem !=null)
            {
                HyperLink hlkItem = (HyperLink)e.Item.FindControl("hlkItem");
                hlkItem.NavigateUrl = cartItem.Product.ProductUrl;
                if (cartItem.Product.ProductName != null)
                {
                    hlkItem.Text = cartItem.Product.ProductName;
                    hlkItem.Style.Add("text-decoration", "none");
                }
                else 
                {
                    hlkItem.Text = "Name Deleted";
                    hlkItem.Style.Add("text-decoration", "none");
                }

                Label lblItemNumber = (Label)e.Item.FindControl("lblItemNumber");
                if (cartItem.Product.ItemNumber != null)
                    lblItemNumber.Text = cartItem.Product.ItemNumber;
                else
                    lblItemNumber.Text = "Deleted";
                
                Label lblItemPrice = (Label)e.Item.FindControl("lblItemPrice");
                lblItemPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(cartItem.UnitPrice);

                Label lblQuantity = (Label)e.Item.FindControl("lblQuantity");
                lblQuantity.Text = cartItem.Quantity.ToString();

                Label lblItemTotal = (Label)e.Item.FindControl("lblItemTotal");
                lblItemTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(cartItem.ItemTotal);
            }

        }
        else if (e.Item.ItemType == ListItemType.Footer)
        {
        }
    }
    protected void imbPrintInvoice_Click(object sender, EventArgs e)
    {
        imbPrintInvoice.Visible = false;
        PrintInvoice();
        Session["PrintOrderList"] = null;
    }

    protected void PrintInvoice()
    {
        string script = "<script type=\"text/javascript\" language=\"javascript\">window.print();</script>";
        RegisterClientScriptBlock("printscript", script);
    }
    protected void rptGiftCertificate_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            GiftItem giftItem = (GiftItem)e.Item.DataItem;
            
            Label lblGiftName = (Label)e.Item.FindControl("lblGiftName");
            lblGiftName.Text = "Name: " + giftItem.ToName+"  "+ "Email: " + giftItem.ToEmailAddress;

            Label lblGiftItemPrice = (Label)e.Item.FindControl("lblGiftItemPrice");
            lblGiftItemPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(giftItem.UnitPrice);

            Label lblGiftQuantity = (Label)e.Item.FindControl("lblGiftQuantity");
            lblGiftQuantity.Text = giftItem.Quantity.ToString();

            Label lblGiftItemTotal = (Label)e.Item.FindControl("lblGiftItemTotal");
            lblGiftItemTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(giftItem.ItemTotal);

        }
        else if (e.Item.ItemType == ListItemType.Footer)
        {

        }

    }   
}
